﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace peonwar
{
    class KbHandlerIP : KbHandler
    {
   
        private bool shouldputspace = false;


        public KbHandlerIP(SpriteFont font, Color color, Vector2 position, int max) : base(font, color, position, max, "")
        {
            lastPressedKeys = new Keys[0];
            this.Position = position;
            this.Color = color;
            this.Font = font;
            this.Max = max;
            this.bar = "";
        }

     
    
        public override void Update(int highticks)
        {
         
           
            KeyboardState kbState = Keyboard.GetState();
            Keys[] pressedKeys = kbState.GetPressedKeys();

            foreach (Keys key in pressedKeys)
            {
                char a;
                char.TryParse(key.ToString(), out a);
                
                if (!lastPressedKeys.Contains(key) )
                {
                    User.AdvertList.Clear();

                    if (key == Keys.Back && stock.Length != 0)
                    {
                        if (stock.Length == 6 || stock.Length == 12 || stock.Length == 18)
                        stock = stock.Substring(0, stock.Length - 4);
                        else stock = stock.Substring(0, stock.Length - 1);
                    }
                    else
                        if (Font.MeasureString(stock).X < Max && stock.Length < 21)
                        {

                            #region QWERTY
                            if (!SettingManager.CurrentProfil.azerty)
                            {
                                if (!kbState.IsKeyDown(Keys.LeftShift))
                                {
                                    switch (key)
                                    {
                                        case Keys.NumPad0:
                                        case Keys.D0:
                                            stock += '0'; break;
                                        case Keys.NumPad1:
                                        case Keys.D1:
                                            stock += '1'; break;
                                        case Keys.NumPad2:
                                        case Keys.D2:
                                            stock += '2'; break;
                                        case Keys.NumPad3:
                                        case Keys.D3:
                                            stock += '3'; break;
                                        case Keys.NumPad4:
                                        case Keys.D4:
                                            stock += '4'; break;
                                        case Keys.NumPad5:
                                        case Keys.D5:
                                            stock += '5'; break;
                                        case Keys.NumPad6:
                                        case Keys.D6:
                                            stock += '6'; break;
                                        case Keys.NumPad7:
                                        case Keys.D7:
                                            stock += '7'; break;
                                        case Keys.NumPad8:
                                        case Keys.D8:
                                            stock += '8'; break;
                                        case Keys.NumPad9:
                                        case Keys.D9:
                                            stock += '9'; break;

                                    }
                                }

                            }
                            #endregion

                            #region AZERtY
                            else
                            {
                                if (!kbState.IsKeyDown(Keys.LeftShift))
                                {
                                    switch (key)
                                    {
                                        case Keys.NumPad0:
                                            stock += '0'; break;
                                        case Keys.NumPad1:
                                            stock += '1'; break;
                                        case Keys.NumPad2:
                                            stock += '2'; break;
                                        case Keys.NumPad3:
                                            stock += '3'; break;
                                        case Keys.NumPad4:
                                            stock += '4'; break;
                                        case Keys.NumPad5:
                                            stock += '5'; break;
                                        case Keys.NumPad6:
                                            stock += '6'; break;
                                        case Keys.NumPad7:
                                            stock += '7'; break;
                                        case Keys.NumPad8:
                                            stock += '8'; break;
                                        case Keys.NumPad9:
                                            stock += '9'; break;
                                    }
                                }
                                if (kbState.IsKeyDown(Keys.LeftShift))
                                {


                                    switch (key)
                                    {
                                        case Keys.D1:
                                            stock += '1'; break;
                                        case Keys.D2:
                                            stock += '2'; break;
                                        case Keys.D3:
                                            stock += '3'; break;
                                        case Keys.D4:
                                            stock += '4'; break;
                                        case Keys.D5:
                                            stock += '5'; break;
                                        case Keys.D6:
                                            stock += '6'; break;
                                        case Keys.D7:
                                            stock += '7'; break;
                                        case Keys.D8:
                                            stock += '8'; break;
                                        case Keys.D9:
                                            stock += '9'; break;
                                        case Keys.D0:
                                            stock += '0'; break;
                                    }
                                }
                            }

                        }
                            #endregion

                    if (stock.Length == 3 || stock.Length == 9 || stock.Length == 15)
                        shouldputspace = true;
                    else shouldputspace = false;

                    if (shouldputspace)
                        stock += "   ";
                }
                
            }

            lastPressedKeys = pressedKeys;

            if (highticks % 30 == 0)
            {
                if (bar == "")
                {
                    if (stock.Length != 15)
                    {
                        if (stock.Length == 3 || stock.Length == 7 || stock.Length == 11)
                            bar = " |";
                        else bar = "|";
                    }
                }
                else
                    bar = "";
            }
        }


   
    }
}